package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.SignOn;
import org.csu.mypetstore.persistence.AccountDao;
import org.csu.mypetstore.persistence.impl.AccountDaoImpl;
import org.springframework.transaction.annotation.Transactional;

public class AccountService {
    AccountDao dao = new AccountDaoImpl();
    public SignOn login(SignOn user) {
        return dao.findUserByUsernameAndPassword(user.getUsername(),user.getPassword());
    }
    public Account getAccount(String username) {
        return dao.getAccountByUsername(username);
    }

    public Account getAccount(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return dao.getAccountByUsernameAndPassword(account);
    }

    @Transactional
    public void insertAccount(Account account) {
        dao.insertAccount(account);
        dao.insertProfile(account);
        dao.insertSignon(account);
    }

    @Transactional
    public void updateAccount(Account account) {
        dao.updateAccount(account);
        dao.updateProfile(account);

        if (account.getPassword() != null && account.getPassword().length() > 0) {
            dao.updateSignon(account);
        }
    }

    public boolean is_exit(String username) {
        if (dao.is_exit(username)!=null){
            return true;
        }
        return false;
    }
}
